package com.kfm.service;

import com.kfm.dao.StudentDao;
import com.kfm.model.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentService {

    StudentDao dao;
    public StudentService() {
        // 1. 加载配置文件
        InputStream in = null;
        try {
            in = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 2. 创建SqlSession的工厂构建者对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        // 3. 创建SqlSessionFactory对象
        SqlSessionFactory factory = sqlSessionFactoryBuilder.build(in);
        // 4. 创建SqlSession对象
        SqlSession sqlSession = factory.openSession();
        // 5. 创建接口的实现类对象  通过代理创建接口的实现类对象
        dao = sqlSession.getMapper(StudentDao.class);
    }

    public List<Student> findAll() {
        return dao.selectAll();
    }

    public Student findById(int id) {
        return dao.selectById(id);
    }

    public List<Student> findByName() {
        return dao.selectByName("");
    }


}
